﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Web.Script.Serialization;
namespace ECube.Web.CMS.Models
{
    public class PageProfile
    {
        
        private List<WidgetContainer> _WidgetContainers;
       
        public PageProfile()
        { 
            IsDesign = false;
        }

        public Boolean IsDesign{get;set;}

        public List<WidgetContainer> WidgetContainers
        {
            get
            {
                if (_WidgetContainers == null)
                {
                    _WidgetContainers = new List<WidgetContainer>();
                }
                return _WidgetContainers;
            }
            set
            {
                _WidgetContainers = value;
            }
        }

        public String ToJson()
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            string s = jss.Serialize(this);            
            return s; 
        }
        public static PageProfile FromJson(string json)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            PageProfile p = jss.Deserialize<PageProfile>(json);

            return p;                                  
        }

        public String ToXML()
        {

            using (MemoryStream ms = new MemoryStream())
            {
                XmlSerializer xs = new XmlSerializer(this.GetType());
                IsDesign = false;
                xs.Serialize(ms, this);
                string sXml = Encoding.UTF8.GetString(ms.ToArray());
                return sXml;
            }


        }

        public static PageProfile FromXML(String xml)
        {
            if (String.IsNullOrEmpty(xml))
            {
                return null;
            }
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            {
                XmlSerializer xs = new XmlSerializer(typeof(PageProfile));

                PageProfile page = xs.Deserialize(ms) as PageProfile;
                return page;
            }

        }
    }
}
